﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

using WorkOrderPackages.Models.WorkOrderPackage;
using WorkOrderPackages.Models.ContractServicePackage;
using WorkOrderPackages.Models.WaypointPackage;
using WorkOrderPackages.Models.LineItemPackage;
using WorkOrderPackages.Models.CommonPackage;

namespace CoreServicesAPI_Tests
{
    class Program
    {
        static void Main(string[] args)
        {
            string fromZip = "55347";
            string toZip = "30008";
            double weight = 150.0;

            double quotedRate = GetCoreServiceQuote(fromZip, toZip, weight).Result;

            Console.WriteLine("The rate to ship {0} pounds from Zip {1} to Zip {2} is ${3} USD ", weight, fromZip, toZip, quotedRate.ToString());
            Console.ReadLine();
        }

        static async Task<double> GetCoreServiceQuote(string fromZip, string toZip, double weight)
        {
            WorkOrder work_order = new WorkOrder();

            work_order.Services = new List<ContractService>() 
            {
                new ContractService()
                {
                    Name = "Shipment Delivery Quote",
                    Type = new ContractServiceType() { Name = "Delivery" },
                    Manifest = new ContractServiceManifest()
                    {
                        Metrics = new Metric()
                            {
                                Weight = new MetricMeasurement()
                                    {
                                        Measurement = weight, UnitOfMeasurement = new UnitOfMeasurement() {Name = "POUND" }
                                    }
                            }
                    },

                    Waypoints =
                        new List<Waypoint>()
                            {
                                new Waypoint()
                                {
                                    Type = new WaypointType() { Name ="PICKUP" },
                                    Address = new WaypointAddress() { PostalCode = fromZip },
                                },
                                new Waypoint()
                                {
                                    Type = new WaypointType() { Name ="DROPOFF" },
                                    Address = new WaypointAddress() { PostalCode = toZip },
                                }
                            }
                }
            };

            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://Spider3pd.cloudapp.net/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage httpResponse = await client.PostAsJsonAsync("api/quote", work_order);
            double shipmentQuote = double.NaN;
            if (httpResponse.IsSuccessStatusCode)
            {
                Stream quoteResponseStream = await httpResponse.Content.ReadAsStreamAsync();
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(WorkOrder));
                WorkOrder quoteResponse = (WorkOrder)jsonSerializer.ReadObject(quoteResponseStream);

                shipmentQuote = quoteResponse.MonetaryValues.First().Value;
            }

            return shipmentQuote;
        }
    }
}
